<?php
namespace App\Admin\Controllers;

use App\Admin\Repositories\BoxPreferential;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Support\Str;
use Dcat\Admin\Support\JavaScript;
use Dcat\Admin\Http\Controllers\AdminController;

class BoxPreferentialController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BoxPreferential(), function (Grid $grid) {
            $grid->column('id')->sortable();
            // $grid->column('user_id');
            $grid->column('user.username', '姓名')->display(function ($username) {
                return $username;
            });
            $grid->column('type')->using(['0' => '积分', '1' => '礼盒']);
            $grid->column('image','优惠券')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $grid->column('content');
            $grid->column('amount');
            $grid->column('create_date');
            $grid->column('end_date');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BoxPreferential(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('content');
            $show->field('user_id');
            $show->field('amount');
            $show->field('create_date');
            $show->field('end_date');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BoxPreferential(), function (Form $form) {
            $form->display('id');
            $form->select('type')->required()->options([0 => '积分', 1 => '礼盒'])->default();
            $form->select('user_id','学员')->options([1 => '王浩浩', 2 => '王浩浩', 3 => '王浩浩',4=>'王暄炀',5 => '任小粟']);
            $form->number('amount');
            $form->date('create_date')->required()->format('YYYY-MM-DD')->default(date('Y-m-d'));
            $form->date('end_date')->required()->format('YYYY-MM-DD');
            $form->image('image','优惠券')->disk('public')->accept('jpg,png,gif,jpeg')->maxSize(1024)->required()->autoUpload()->help('大小不要超过1024K');
            $form->markdown('content')->required()->options([
                'onpreviewing' => JavaScript::make(
                    <<<JS
            function() {
                // console.log("onpreviewing =>", this, this.id, this.settings);
                // on previewing you can custom css .editormd-preview-active
            }
            JS
                ),
            ])->disk('public');
        });
    }
}
